package gui.components.map;

import gnu.cajo.utils.extra.TransparentItemProxy;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import combat.protocol.data.DoublePoint;
import combat.protocol.data.RemoteList;
import combat.protocol.interfaces.OnboardWeaponSystemInt;
import combat.protocol.interfaces.VehicleInt;
import simulation.Connection;

public class PopupMenu extends JPanel {
	private static final long serialVersionUID = 1L;
	
	public JPopupMenu rightClick;
	private double x = 0;
	private double y = 0;
	
	public void setX(double _x){
		x = _x;
	}
	public double returnX(){
		return x;
	}
	public void setY(double _y){
		y = _y;
	}
	public double returnY(){
		return y;
	}

	public PopupMenu(){	
		//Create the popup menu
		rightClick = new JPopupMenu();
		rightClick.setLabel("Actions");
		rightClick.setBorder(BorderFactory.createLineBorder(Color.black));
		//Add actionlistener to show the menu
		addMouseListener(new MousePopupListener());
	}
		
	//Create the actionlistener for menuitems
	ActionListener popupListener = new ActionListener() {
		public void actionPerformed(ActionEvent event) {
			//Get x / y coordinates
			double x = returnX();
			double y = returnY();
			
			//Get instance of vehicleInt
			Object vehicleObject = Connection.getMe().getUserVehicle_Remote();
			VehicleInt vehicle = (VehicleInt) TransparentItemProxy.getItem(vehicleObject, new Class[]
            {
				VehicleInt.class
             });
			
			ArrayList<OnboardWeaponSystemInt> localSystems = new ArrayList <OnboardWeaponSystemInt>();
            RemoteList remoteSystems = new RemoteList();
			try {
                remoteSystems = Connection.getMyVehicle().getOnboardSystems_Remote();
                
                if(remoteSystems != null) {
                	for(Object systemRemote : remoteSystems) {
                		OnboardWeaponSystemInt systems = (OnboardWeaponSystemInt)TransparentItemProxy.getItem(systemRemote, new Class[] { OnboardWeaponSystemInt.class });
                		if(systems.getOnboardSystemType().toString() == "WEAPON"){
                    		localSystems.add(systems);
                		}
                    }
                }
           } catch(Exception e){
                System.out.println(e.getMessage());
           }
			
			if(event.getActionCommand() == "Move here"){
				//New move order
				vehicle.orderMove(
						0,
						new DoublePoint(x, y),
						100
					);
			} else if(event.getActionCommand() == "Missile" ){
				if(localSystems != null && vehicle.getVehicleType().toString() == "FRIGATE"){		
					vehicle.orderAttack(0, new DoublePoint(x, y), localSystems.get(1));
				}
			} else if(event.getActionCommand() == "Torpedo" ){
				if(localSystems != null && vehicle.getVehicleType().toString() == "SUBMARINE"){		
					vehicle.orderAttack(0, new DoublePoint(x, y), localSystems.get(3));
				}
			} else if(event.getActionCommand() == "Cannon" ){
				if(localSystems != null && vehicle.getVehicleType().toString() == "FRIGATE"){		
					vehicle.orderAttack(0, new DoublePoint(x, y), localSystems.get(2));
				}
			} else if(event.getActionCommand() == "Goalkeeper" ){
				if(localSystems != null && vehicle.getVehicleType().toString() == "FRIGATE"){		
					vehicle.orderAttack(0, new DoublePoint(x, y), localSystems.get(0));
				}
			} else {
				return;
			}
		}
	};
    // An inner class to check whether mouse events are the popup trigger
    class MousePopupListener extends MouseAdapter {
        public void mousePressed(MouseEvent e) { checkPopup(e); }
        public void mouseClicked(MouseEvent e) { checkPopup(e); }
        public void mouseReleased(MouseEvent e) { checkPopup(e); }

        private void checkPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
            	//Show popup menu
                rightClick.show(PopupMenu.this, e.getX(), e.getY());
                //Set the x and y coordinates
                setX(e.getX());
                setY(e.getY());
            }
        }
    }
    public void newMenuItem(String name){
		//Create the popup menu items
		JMenuItem newItem = new JMenuItem(name);
		//Add the actionlistener
		newItem.addActionListener(popupListener);
		//Add to the menu
		rightClick.add(newItem);	
    }
    //Add seperator
    public void addSeperator() {
    	rightClick.addSeparator();
    }
}
